package com.turman.builibrary.banner.indicator;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.turman.blibrary.util.BDisplayUtil;
import com.turman.builibrary.R;

public class BCircleIndicator extends FrameLayout implements IBIndicator<FrameLayout> {
    @DrawableRes
    private int mPointNormal = R.drawable.shape_point_normal;
    @DrawableRes
    private int mPointSelected = R.drawable.shape_point_select;

    //指示器左右间距
    private int leftRightPadding = 0;
    //指示器上下间距
    private int topBottomPadding = 0;

    public BCircleIndicator(@NonNull Context context) {
        this(context, null);
    }

    public BCircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BCircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        leftRightPadding = BDisplayUtil.dp2px(5f, getResources());
        topBottomPadding = BDisplayUtil.dp2px(15f, getResources());
    }

    @Override
    public FrameLayout getLayout() {
        return this;
    }

    @Override
    public void onInflate(int count) {
        removeAllViews();
        if (count <= 0) {
            return;
        }

        LinearLayout groupView = new LinearLayout(getContext());
        groupView.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_VERTICAL;
        params.setMargins(leftRightPadding, topBottomPadding, leftRightPadding, topBottomPadding);
        for (int i = 0; i < count; i++) {
            ImageView item = new ImageView(getContext());
            item.setLayoutParams(params);
            if (i == 0) {
                item.setImageResource(mPointSelected);
            } else {
                item.setImageResource(mPointNormal);
            }
            groupView.addView(item);
        }
        LayoutParams parentParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        parentParams.gravity = Gravity.CENTER | Gravity.BOTTOM;
        addView(groupView, parentParams);
    }

    @Override
    public void onPointChange(int current, int count) {
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            ImageView item = (ImageView) viewGroup.getChildAt(i);
            if (i == current) {
                item.setImageResource(mPointSelected);
            } else {
                item.setImageResource(mPointNormal);
            }
            item.requestLayout();
        }
    }
}
